﻿using System; 
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using Assessment.DBUtility;
namespace Assessment.DAL  
{
	 	//T_Assessment
		public partial class T_AssessmentDAL
	{
   		     
		public bool Exists(int AssessmentId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from T_Assessment");
			strSql.Append(" where ");
			                                       strSql.Append(" AssessmentId = @AssessmentId  ");
                            			SqlParameter[] parameters = {
					new SqlParameter("@AssessmentId", SqlDbType.Int,4)
			};
			parameters[0].Value = AssessmentId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
		
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Assessment.Model.T_Assessment model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into T_Assessment(");			
            strSql.Append("MedicalRecordId,Abstract1,Abstract2,Abstract3,Abstract4,Abstract5,Abstract6,Abstract7,Abstract8,Abstract9,Abstract10,Abstract11,Abstract12,Report,PhysicalExamination,ImagingExamination,SpecialCircumstances,CreateUserId,CreateDate,UpdateUserId,UpdateDate,Valid");
			strSql.Append(") values (");
            strSql.Append("@MedicalRecordId,@Abstract1,@Abstract2,@Abstract3,@Abstract4,@Abstract5,@Abstract6,@Abstract7,@Abstract8,@Abstract9,@Abstract10,@Abstract11,@Abstract12,@Report,@PhysicalExamination,@ImagingExamination,@SpecialCircumstances,@CreateUserId,@CreateDate,@UpdateUserId,@UpdateDate,@Valid");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@MedicalRecordId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Abstract1", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract2", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract3", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract4", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract5", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract6", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract7", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract8", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract9", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract10", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract11", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract12", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Report", SqlDbType.VarChar,2000) ,            
                        new SqlParameter("@PhysicalExamination", SqlDbType.VarChar,2000) ,            
                        new SqlParameter("@ImagingExamination", SqlDbType.VarChar,2000) ,            
                        new SqlParameter("@SpecialCircumstances", SqlDbType.VarChar,2000) ,            
                        new SqlParameter("@CreateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateDate", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UpdateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdateDate", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@Valid", SqlDbType.Int,4)             
              
            };
			            
            parameters[0].Value = model.MedicalRecordId;                        
            parameters[1].Value = model.Abstract1;                        
            parameters[2].Value = model.Abstract2;                        
            parameters[3].Value = model.Abstract3;                        
            parameters[4].Value = model.Abstract4;                        
            parameters[5].Value = model.Abstract5;                        
            parameters[6].Value = model.Abstract6;                        
            parameters[7].Value = model.Abstract7;                        
            parameters[8].Value = model.Abstract8;                        
            parameters[9].Value = model.Abstract9;                        
            parameters[10].Value = model.Abstract10;                        
            parameters[11].Value = model.Abstract11;                        
            parameters[12].Value = model.Abstract12;                        
            parameters[13].Value = model.Report;                        
            parameters[14].Value = model.PhysicalExamination;                        
            parameters[15].Value = model.ImagingExamination;                        
            parameters[16].Value = model.SpecialCircumstances;                        
            parameters[17].Value = model.CreateUserId;                        
            parameters[18].Value = model.CreateDate;                        
            parameters[19].Value = model.UpdateUserId;                        
            parameters[20].Value = model.UpdateDate;                        
            parameters[21].Value = model.Valid;                        
			   
			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			if (obj == null)
			{
				return 0;
			}
			else
			{
				                    
            	return Convert.ToInt32(obj);
                                                                  
			}			   
            			
		}
		
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Assessment.Model.T_Assessment model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update T_Assessment set ");
			                                                
            strSql.Append(" MedicalRecordId = @MedicalRecordId , ");                                    
            strSql.Append(" Abstract1 = @Abstract1 , ");                                    
            strSql.Append(" Abstract2 = @Abstract2 , ");                                    
            strSql.Append(" Abstract3 = @Abstract3 , ");                                    
            strSql.Append(" Abstract4 = @Abstract4 , ");                                    
            strSql.Append(" Abstract5 = @Abstract5 , ");                                    
            strSql.Append(" Abstract6 = @Abstract6 , ");                                    
            strSql.Append(" Abstract7 = @Abstract7 , ");                                    
            strSql.Append(" Abstract8 = @Abstract8 , ");                                    
            strSql.Append(" Abstract9 = @Abstract9 , ");                                    
            strSql.Append(" Abstract10 = @Abstract10 , ");                                    
            strSql.Append(" Abstract11 = @Abstract11 , ");                                    
            strSql.Append(" Abstract12 = @Abstract12 , ");                                    
            strSql.Append(" Report = @Report , ");                                    
            strSql.Append(" PhysicalExamination = @PhysicalExamination , ");                                    
            strSql.Append(" ImagingExamination = @ImagingExamination , ");                                    
            strSql.Append(" SpecialCircumstances = @SpecialCircumstances , ");                                    
            strSql.Append(" CreateUserId = @CreateUserId , ");                                    
            strSql.Append(" CreateDate = @CreateDate , ");                                    
            strSql.Append(" UpdateUserId = @UpdateUserId , ");                                    
            strSql.Append(" UpdateDate = @UpdateDate , ");                                    
            strSql.Append(" Valid = @Valid  ");            			
			strSql.Append(" where AssessmentId=@AssessmentId ");
						
SqlParameter[] parameters = {
			            new SqlParameter("@AssessmentId", SqlDbType.Int,4) ,            
                        new SqlParameter("@MedicalRecordId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Abstract1", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract2", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract3", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract4", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract5", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract6", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract7", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract8", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract9", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract10", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract11", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Abstract12", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Report", SqlDbType.VarChar,2000) ,            
                        new SqlParameter("@PhysicalExamination", SqlDbType.VarChar,2000) ,            
                        new SqlParameter("@ImagingExamination", SqlDbType.VarChar,2000) ,            
                        new SqlParameter("@SpecialCircumstances", SqlDbType.VarChar,2000) ,            
                        new SqlParameter("@CreateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateDate", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UpdateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdateDate", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@Valid", SqlDbType.Int,4)             
              
            };
						            
            parameters[0].Value = model.AssessmentId;                        
            parameters[1].Value = model.MedicalRecordId;                        
            parameters[2].Value = model.Abstract1;                        
            parameters[3].Value = model.Abstract2;                        
            parameters[4].Value = model.Abstract3;                        
            parameters[5].Value = model.Abstract4;                        
            parameters[6].Value = model.Abstract5;                        
            parameters[7].Value = model.Abstract6;                        
            parameters[8].Value = model.Abstract7;                        
            parameters[9].Value = model.Abstract8;                        
            parameters[10].Value = model.Abstract9;                        
            parameters[11].Value = model.Abstract10;                        
            parameters[12].Value = model.Abstract11;                        
            parameters[13].Value = model.Abstract12;                        
            parameters[14].Value = model.Report;                        
            parameters[15].Value = model.PhysicalExamination;                        
            parameters[16].Value = model.ImagingExamination;                        
            parameters[17].Value = model.SpecialCircumstances;                        
            parameters[18].Value = model.CreateUserId;                        
            parameters[19].Value = model.CreateDate;                        
            parameters[20].Value = model.UpdateUserId;                        
            parameters[21].Value = model.UpdateDate;                        
            parameters[22].Value = model.Valid;                        
            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int AssessmentId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from T_Assessment ");
			strSql.Append(" where AssessmentId=@AssessmentId");
						SqlParameter[] parameters = {
					new SqlParameter("@AssessmentId", SqlDbType.Int,4)
			};
			parameters[0].Value = AssessmentId;


			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string AssessmentIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from T_Assessment ");
			strSql.Append(" where ID in ("+AssessmentIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
				
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Assessment.Model.T_Assessment GetModel(int AssessmentId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select AssessmentId, MedicalRecordId, Abstract1, Abstract2, Abstract3, Abstract4, Abstract5, Abstract6, Abstract7, Abstract8, Abstract9, Abstract10, Abstract11, Abstract12, Report, PhysicalExamination, ImagingExamination, SpecialCircumstances, CreateUserId, CreateDate, UpdateUserId, UpdateDate, Valid  ");			
			strSql.Append("  from T_Assessment ");
			strSql.Append(" where AssessmentId=@AssessmentId");
						SqlParameter[] parameters = {
					new SqlParameter("@AssessmentId", SqlDbType.Int,4)
			};
			parameters[0].Value = AssessmentId;

			
			Assessment.Model.T_Assessment model=new Assessment.Model.T_Assessment();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			
			if(ds.Tables[0].Rows.Count>0)
			{
												if(ds.Tables[0].Rows[0]["AssessmentId"].ToString()!="")
				{
					model.AssessmentId=int.Parse(ds.Tables[0].Rows[0]["AssessmentId"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["MedicalRecordId"].ToString()!="")
				{
					model.MedicalRecordId=int.Parse(ds.Tables[0].Rows[0]["MedicalRecordId"].ToString());
				}
																																				model.Abstract1= ds.Tables[0].Rows[0]["Abstract1"].ToString();
																																model.Abstract2= ds.Tables[0].Rows[0]["Abstract2"].ToString();
																																model.Abstract3= ds.Tables[0].Rows[0]["Abstract3"].ToString();
																																model.Abstract4= ds.Tables[0].Rows[0]["Abstract4"].ToString();
																																model.Abstract5= ds.Tables[0].Rows[0]["Abstract5"].ToString();
																																model.Abstract6= ds.Tables[0].Rows[0]["Abstract6"].ToString();
																																model.Abstract7= ds.Tables[0].Rows[0]["Abstract7"].ToString();
																																model.Abstract8= ds.Tables[0].Rows[0]["Abstract8"].ToString();
																																model.Abstract9= ds.Tables[0].Rows[0]["Abstract9"].ToString();
																																model.Abstract10= ds.Tables[0].Rows[0]["Abstract10"].ToString();
																																model.Abstract11= ds.Tables[0].Rows[0]["Abstract11"].ToString();
																																model.Abstract12= ds.Tables[0].Rows[0]["Abstract12"].ToString();
																																model.Report= ds.Tables[0].Rows[0]["Report"].ToString();
																																model.PhysicalExamination= ds.Tables[0].Rows[0]["PhysicalExamination"].ToString();
																																model.ImagingExamination= ds.Tables[0].Rows[0]["ImagingExamination"].ToString();
																																model.SpecialCircumstances= ds.Tables[0].Rows[0]["SpecialCircumstances"].ToString();
																												if(ds.Tables[0].Rows[0]["CreateUserId"].ToString()!="")
				{
					model.CreateUserId=int.Parse(ds.Tables[0].Rows[0]["CreateUserId"].ToString());
				}
																																				model.CreateDate= ds.Tables[0].Rows[0]["CreateDate"].ToString();
																												if(ds.Tables[0].Rows[0]["UpdateUserId"].ToString()!="")
				{
					model.UpdateUserId=int.Parse(ds.Tables[0].Rows[0]["UpdateUserId"].ToString());
				}
																																				model.UpdateDate= ds.Tables[0].Rows[0]["UpdateDate"].ToString();
																												if(ds.Tables[0].Rows[0]["Valid"].ToString()!="")
				{
					model.Valid=int.Parse(ds.Tables[0].Rows[0]["Valid"].ToString());
				}
																														
				return model;
			}
			else
			{
				return null;
			}
		}
		
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM T_Assessment ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM T_Assessment ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

   
   		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_Assessment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
		}
		
		
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.MenuId desc");
			}
			strSql.Append(")AS Row, T.*  from T_Assessment T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}
	}
}

